## classify issues in the cces 
## as liberal/conservative
## based on responses to
## ideology questions

rm(list = ls())

library(tidyverse)

source('bootstrap_funs.R')

# read in and clean cces data

cces_allyears <- readRDS('cces_data/cces_allyears.RDS') %>% 
  filter(year != 2010)

cces_allyears <- cces_allyears %>% 
  subset_unique_questions()

cces_allyears <- cces_allyears %>%   # drops leaners and weird cross-party identifiers from being d or r
  mutate(pid3 = case_when(pid3 == 'Democrat' & pid7 %in% c('Strong Democrat', 'Not very strong Democrat') ~ 'Democrat',
                          pid3 == 'Republican' & pid7 %in% c('Strong Republican', 'Not very strong Republican') ~ 'Republican',
                          pid3 != 'Independent' & pid7 %in% c('Lean Democrat', 'Lean Republican') ~ 'Independent (lean edit)',
                          TRUE ~ pid3),
         ideo_score = case_when(ideo5 %in% c('Liberal', 'Very liberal') ~ -1,
                                ideo5 %in% c('Very Conservative', 'Conservative') ~ 1,
                                ideo5 == 'Moderate' ~ 0,
                                TRUE ~ NA_real_))

issuetopics <- read.csv('issuetopics.csv')
issuetopics$ideology <- NA

for (i in 1:nrow(issuetopics)) {
  if (i %% floor(nrow(issuetopics)/10) == 0) cat('|')
  cces.tmp <- cces_allyears %>% 
    filter(year == issuetopics$year[i],
           question == issuetopics$question[i])
  
  issuetopics$ideology[i] <- cor(cces.tmp$opinion, cces.tmp$ideo_score, use = 'complete')
}

issuetopics$liberal <- as.numeric(issuetopics$ideology < 0)

write.csv(issuetopics %>% arrange(ideology), 'issue_ideology.csv', row.names = FALSE)
